using System;
using System.Collections.Generic;
using System.Reflection;
using NodeCanvas.Variables;
using UnityEngine;

namespace NodeCanvas.Conditions
{
	[Category("✫ Script Control")]
	[Description("Call a function with none or one parameter on a script and return whether or not the return value is equal to the check value")]
	[AgentType(typeof(Transform))]
	public class CheckFunction : ConditionTask
	{
		public BBVariableSet paramValue1 = new BBVariableSet();

		public BBVariableSet checkSet = new BBVariableSet();

		[SerializeField]
		private string methodName;

		[SerializeField]
		private string scriptName;

		[SerializeField]
		private CompareMethod comparison;

		private Component script;

		private MethodInfo method;

		protected override string info
		{
			get
			{
				if (string.IsNullOrEmpty(methodName))
				{
					return "No Method Selected";
				}
				string empty = string.Empty;
				empty += ((paramValue1.selectedType == null) ? string.Empty : paramValue1.selectedBBVariable.ToString());
				return string.Format("{0}.{1}({2}){3}", base.agentInfo, methodName, empty, (checkSet.selectedType != typeof(bool)) ? (TaskTools.GetCompareString(comparison) + checkSet.selectedBBVariable) : string.Empty);
			}
		}

		protected override string OnInit()
		{
			script = base.agent.GetComponent(scriptName);
			if (script == null)
			{
				return "Missing Component '" + scriptName + "' on Agent '" + base.agent.gameObject.name + "'";
			}
			List<Type> list = new List<Type>();
			if (paramValue1.selectedType != null)
			{
				list.Add(paramValue1.selectedType);
			}
			method = script.GetType().NCGetMethod(methodName, list.ToArray());
			if (method == null)
			{
				return "Missing Method Info";
			}
			return null;
		}

		protected override bool OnCheck()
		{
			object[] parameters = null;
			if (paramValue1.selectedType != null)
			{
				parameters = new object[1] { paramValue1.objectValue };
			}
			if (checkSet.selectedType == typeof(float) || checkSet.selectedType == typeof(int))
			{
				return TaskTools.Compare((IComparable)method.Invoke(script, parameters), (IComparable)checkSet.objectValue, comparison);
			}
			return object.Equals(method.Invoke(script, parameters), checkSet.objectValue);
		}
	}
}
